package com.ruoyi.utils;


import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * @author xupeiqi
 */
public class MyFileUploadUtils {

    private static final List<String> SATISFIED_EXT_NAME = Arrays.asList(
            "txt", "doc", "docx", "xls", "xlsx", "jpg", "jpeg", "png", "bmp", "tif", "gif", "webp", "svg", "pdf", "mp4"
    );

    public static void validateFileFormat(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String extName = FileUtil.extName(filename);
        if (!SATISFIED_EXT_NAME.contains(extName.toLowerCase())) {
            throw new ServiceException("不支持该文件格式上传");
        }
        long fileSize = file.getSize();
        if ((fileSize / 1024 / 1024) > 50) {
            throw new ServiceException("上传文件过大，请上传50M以下的文件");
        }
    }
}
